package com.nidu.demo.oauth2.executor.command;

import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.oauth2.dto.OAuth2ClientUpdateCmd;
import com.nidu.demo.oauth2.ability.OAuth2ClientDomainService;
import com.nidu.demo.oauth2.convertor.OAuth2ClientConvertor;
import com.nidu.demo.oauth2.dto.OAuth2ClientCO;
import com.nidu.demo.oauth2.gateway.OAuth2ClientGateway;
import com.nidu.demo.oauth2.model.OAuth2Client;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class OAuth2ClientUpdateCmdExe {

    private final OAuth2ClientGateway gateway;

    private final OAuth2ClientDomainService domainService;

    private final OAuth2ClientConvertor convertor;

    public SingleResponse<OAuth2ClientCO> execute(OAuth2ClientUpdateCmd cmd) {
        // 1、转换领域对象
        OAuth2Client oAuth2Client = OAuth2Client.update(cmd.getId(), cmd.getClientCode(), cmd.getClientSecret(), cmd.getScopes(), cmd.getRedirectUri(),
                cmd.getAccessTokenValiditySeconds(), cmd.getRefreshTokenValiditySeconds(), cmd.getApproveValiditySeconds());

        // 2、检查客户端编号是否已存在
        domainService.validateDuplicate(cmd.getClientCode(), cmd.getId());

        // 3、保存数据
        OAuth2Client save = gateway.update(oAuth2Client);
        return SingleResponse.of(convertor.toClientObject(save));
    }

}
